#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , list1(new LinkedList())
    , list2(new LinkedList()) {

    ui->setupUi(this);

    setWindowTitle("链表可视化演示");

    lineEdit = new QLineEdit(this);
    createButton = new QPushButton("创建链表", this);
    intersectionButton = new QPushButton("求交集", this);
    unionButton = new QPushButton("求并集", this);
    differenceButton = new QPushButton("求补集", this);
    resultLabel = new QLabel(this);

    view1 = new QGraphicsView(this);
    view2 = new QGraphicsView(this);
    resultView = new QGraphicsView(this);
    scene1 = new QGraphicsScene(this);
    scene2 = new QGraphicsScene(this);
    resultScene = new QGraphicsScene(this);
    view1->setScene(scene1);
    view2->setScene(scene2);
    resultView->setScene(resultScene);

    selectView1 = new QRadioButton("选择视图1", this);
    selectView2 = new QRadioButton("选择视图2", this);
    selectView1->setChecked(true); // 默认选中视图1

    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(lineEdit);
    hLayout->addWidget(createButton);
    hLayout->addWidget(intersectionButton);
    hLayout->addWidget(unionButton);
    hLayout->addWidget(differenceButton);
    hLayout->addWidget(selectView1);
    hLayout->addWidget(selectView2);
    //hLayout->addWidget(resultLabel);

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addLayout(hLayout);
    vLayout->addWidget(view1);
    vLayout->addWidget(view2);
    vLayout->addWidget(resultView);

    QWidget *centralWidget = new QWidget(this);
    centralWidget->setLayout(vLayout);
    setCentralWidget(centralWidget);

    connect(createButton, &QPushButton::clicked, this, &MainWindow::createOrAddNode);
    connect(intersectionButton, &QPushButton::clicked, this, &MainWindow::showIntersection);
    connect(unionButton, &QPushButton::clicked, this, &MainWindow::showUnion);
    connect(differenceButton, &QPushButton::clicked, this, &MainWindow::showDifference);
    connect(selectView1, &QRadioButton::clicked, this, &MainWindow::handleViewSelection);
    connect(selectView2, &QRadioButton::clicked, this, &MainWindow::handleViewSelection);

    // 初始判断按钮文本
    handleViewSelection();
}

void MainWindow::createOrAddNode() {
    LinkedList* currentList;
    QGraphicsScene* currentScene;

    if (selectView1->isChecked()) {
        currentList = list1;
        currentScene = scene1;
    } else if (selectView2->isChecked()) {
        currentList = list2;
        currentScene = scene2;
    } else {
        return; // No valid selection
    }

    QString input = lineEdit->text();
    QStringList elements = input.split(',');

    for (const QString& element : elements) {
        currentList->append(element.toInt());
    }
    displayList(currentList, currentScene);

    // 如果创建的是第一个链表，自动选中视图2
    if (currentList == list1 && listCount == 0) {
        selectView2->setChecked(true);
        handleViewSelection(); // 更新按钮文本
    }

    listCount++;
}

void MainWindow::showIntersection() {
    LinkedList result = list1->intersection(*list2);
    displayList(&result, resultScene); // Display result in the result view
}

void MainWindow::showUnion() {
    LinkedList result = list1->unionList(*list2);
    displayList(&result, resultScene); // Display result in the result view
}

void MainWindow::showDifference() {
    LinkedList result = list1->difference(*list2);
    displayList(&result, resultScene); // Display result in the result view
}

void MainWindow::displayList(LinkedList* list, QGraphicsScene* scene) {
    clearScene(scene);
    Node* temp = list->head;
    int x = 0;
    int y = -50; // 调整起始的 y 坐标
    int spacing = 130; // 增加间隔
    while (temp) {
        addNodeToScene(temp->data, x, y, temp, scene);
        x += spacing; // Move to the right for the next node with increased spacing
        temp = temp->next;
    }
}

void MainWindow::clearScene(QGraphicsScene* scene) {
    scene->clear();
}

void MainWindow::addNodeToScene(int value, int x, int y, Node* nodePointer, QGraphicsScene* scene) {
    AnimatedRectItem* rect = new AnimatedRectItem(QRectF(x, y, 80, 50), value, nodePointer); // 调整大小并传递值和节点指针
    scene->addItem(rect);

    connect(rect, &AnimatedRectItem::valueChanged, this, &MainWindow::handleNodeValueChanged);

    // 添加动画效果
    QPropertyAnimation* animation = new QPropertyAnimation(rect, "geometry");
    animation->setDuration(500);
    animation->setStartValue(QRectF(x, y - 50, 80, 50)); // 从上方掉落
    animation->setEndValue(QRectF(x, y, 80, 50));
    animation->start(QAbstractAnimation::DeleteWhenStopped);

    // 添加箭头
    if (x > 0) {
        QGraphicsLineItem* line = scene->addLine(x - 130 + 80, y + 25, x, y + 25, QPen(Qt::black));
        QPolygonF arrowHead;
        arrowHead << QPointF(x, y + 25) << QPointF(x - 10, y + 20) << QPointF(x - 10, y + 30);
        scene->addPolygon(arrowHead, QPen(Qt::black), QBrush(Qt::black));
    }
}

void MainWindow::handleNodeValueChanged(int newValue, Node* nodePointer) {
    // 直接更新节点值
    if (nodePointer) {
        nodePointer->data = newValue;
    }

    // 重新显示链表
    LinkedList* currentList;
    QGraphicsScene* currentScene;

    if (selectView1->isChecked()) {
        currentList = list1;
        currentScene = scene1;
    } else if (selectView2->isChecked()) {
        currentList = list2;
        currentScene = scene2;
    } else {
        return; // No valid selection
    }

    displayList(currentList, currentScene);
}

void MainWindow::handleViewSelection() {
    LinkedList* currentList;
    if (selectView1->isChecked()) {
        currentList = list1;
    } else if (selectView2->isChecked()) {
        currentList = list2;
    } else {
        return; // No valid selection
    }

    if (currentList->isEmpty()) {
        createButton->setText("创建链表");
    } else {
        createButton->setText("增加节点");
    }
}

MainWindow::~MainWindow() {
    delete list1;
    delete list2;
    delete ui;
}
